package com.demo.villageresidence.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.villageresidence.dto.Result;
import com.demo.villageresidence.pojo.Activity;
import com.demo.villageresidence.pojo.Admin;
import com.demo.villageresidence.pojo.Tender;
import com.demo.villageresidence.service.AdminService;
import com.demo.villageresidence.service.TenderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/tender")
public class TenderController {

    @Autowired
    private TenderService tenderService;

    @Autowired
    private AdminService adminService;

    @PostMapping("addTender")
    public Result addTender(@RequestBody Tender tender){

        // 设置发布时间
        tender.setTenderTime(LocalDateTime.now());
        // 设置删除状态
        tender.setIsDelete(0);

        if (tender.getTenderDigest().length() >= 200){
            // 摘要内容过长，截取为200个字符
            tender.setTenderDigest(tender.getTenderDigest().substring(0, 200));
        }else {
            tender.setTenderDigest(tender.getTenderDigest());
        }

        if(tender.getTenderStarttime().after(tender.getTenderEndtime()))
            return Result.fail("招标开始时间不能在报名截止时间之前，请检查！");

        tenderService.save(tender);
        return Result.ok("发布成功");
    }

    @GetMapping("getTenderByPage")
    public Result getTenderByPage(@RequestParam(value = "current", defaultValue = "1") Integer current,
                                  @RequestParam(value = "pageSize", defaultValue = "10")  Integer pageSize) {
        // 根据用户查询 日常活动
        Page<Tender> page = tenderService.query().orderByDesc("tender_time")
                .page(new Page<>(current, pageSize));
        // 获取当前页数据
        List<Tender> records = page.getRecords();
        // 获取总条数
        long total = page.getTotal();
        return Result.ok(records,total);
    }


    @GetMapping("getAllTender")
    public Result getAllTender() {
        // 根据用户查询 日常活动
        List<Tender> tenders = tenderService.query().orderByDesc("tender_time").list();
        tenders.forEach(tender -> {
            tender.setAdmin(adminService.getById(tender.getAdminId()));
        });

        return Result.ok(tenders);
    }



    /**
     * 根据id查询招标信息
     * @param id
     * @return
     */
    @GetMapping("getTenderById/{id}")
    public Result getTenderById(@PathVariable("id") Integer id){
        Tender tender = tenderService.getById(id);
        return Result.ok(tender);
    }


    @PutMapping("updateTenderById")
    public Result updateTenderById(@RequestBody Tender tender){

        // 设置发布时间
        tender.setTenderTime(LocalDateTime.now());
        // 设置删除状态
        tender.setIsDelete(0);


        if(tender.getTenderStarttime().after(tender.getTenderEndtime()))
            return Result.fail("招标开始时间不能在报名截止时间之前，请检查！");
        tenderService.updateById(tender);
        return Result.ok("修改成功");
    }


}
